<template>
  <div class="database-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>数据备份</h3>
          <div class="header-actions">
            <el-button type="primary" @click="handleBackup">数据备份</el-button>
            <el-button @click="handleOptimize">数据表优化</el-button>
            <el-button type="danger" @click="handleRepair">数据表修复</el-button>
            <span id="dataTips" class="data-tips">{{ dataTips }}</span>
          </div>
        </div>
      </template>

      <el-table
        :data="tableList"
        border
        v-loading="loading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="数据表" width="200" />
        <el-table-column label="状态" width="100">
          <template #default="scope">
            <span :id="'table_' + scope.row.name" :class="getTableStatusClass(scope.row)">
              {{ getTableStatusText(scope.row) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="engine" label="存储引擎" align="center" width="80" />
        <el-table-column prop="row_format" label="行格式" align="center" width="80" />
        <el-table-column prop="rows" label="行数" align="center" width="60" />
        <el-table-column prop="data_size" label="字节数" align="center" width="80" />
        <el-table-column prop="data_length" label="数据大小" align="center" width="80" />
        <el-table-column prop="comment" label="数据表注释" />
        <el-table-column prop="create_time" label="创建时间" width="160" align="center" />
      </el-table>
    </el-card>

    <!-- 备份进度对话框 -->
    <el-dialog
      v-model="backupDialogVisible"
      title="备份进度"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="backup-progress">
        <p>备份中...，请勿关闭本页面</p>
        <el-progress :percentage="backupProgress" :stroke-width="18" />
        <p>{{ backupMessage }}</p>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import {
    getDatabaseTables as getDatabaseTablesApi,
    backupTables as backupTablesApi,
    optimizeTables as optimizeTablesApi,
    repairTables as repairTablesApi,
  } from '@/api/home/database'

  // 数据相关
  const tableList = ref<any[]>([])
  const loading = ref(false)
  const dataTips = ref('')

  // 备份相关
  const backupDialogVisible = ref(false)
  const backupProgress = ref(0)
  const backupMessage = ref('')

  // 获取数据库表列表
  const getDatabaseTables = async () => {
    try {
      loading.value = true
      const response: any = await getDatabaseTablesApi()

      if (response.code === 0) {
        tableList.value = response.data
        dataTips.value = response.msg
      } else {
        ElMessage.error(response.msg || '获取数据表列表失败')
      }
    } catch (error) {
      ElMessage.error('获取数据表列表失败')
    } finally {
      loading.value = false
    }
  }

  // 处理备份
  const handleBackup = async () => {
    try {
      loading.value = true
      const response: any = await backupTablesApi({})

      if (response.code === 0) {
        ElMessage.success(response.msg)
        await getDatabaseTables()
      } else if (response.code === 2) {
        ElMessageBox.alert(response.msg, '提示', {
          confirmButtonText: '确定',
          type: 'warning',
        })
      } else {
        ElMessage.error(response.msg || '备份失败')
      }
    } catch (error) {
      ElMessage.error('备份失败')
    } finally {
      loading.value = false
    }
  }

  // 处理优化
  const handleOptimize = async () => {
    try {
      loading.value = true
      ElMessage.info('正在优化数据表，请稍候...')

      const response: any = await optimizeTablesApi({})

      if (response.code === 0) {
        ElMessage.success(response.msg)
        await getDatabaseTables()
      } else {
        ElMessage.error(response.msg || '优化失败')
      }
    } catch (error: any) {
      console.error('优化失败:', error)
      ElMessage.error(error.message || '优化失败，请稍后重试')
    } finally {
      loading.value = false
    }
  }

  // 处理修复
  const handleRepair = async () => {
    try {
      loading.value = true
      ElMessage.info('正在修复数据表，请稍候...')

      const response: any = await repairTablesApi({})

      if (response.code === 0) {
        ElMessage.success(response.msg)
        await getDatabaseTables()
      } else {
        ElMessage.error(response.msg || '修复失败')
      }
    } catch (error: any) {
      console.error('修复失败:', error)
      ElMessage.error(error.message || '修复失败，请稍后重试')
    } finally {
      loading.value = false
    }
  }

  // 处理选择变化
  const handleSelectionChange = (selection: any[]) => {
    // 处理选择变化
    console.log('选择变化:', selection)
  }

  // 获取表格状态类
  const getTableStatusClass = (_row: any) => {
    return 'normal'
  }

  // 获取表格状态文本
  const getTableStatusText = (_row: any) => {
    return '正常'
  }

  // 初始化
  onMounted(() => {
    getDatabaseTables()
  })
</script>

<style scoped>
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
  }

  .data-tips {
    font-size: 12px;
    margin-left: 10px;
  }

  .backup-progress {
    text-align: center;
  }

  .success {
    color: #67c23a;
  }

  .table-actions {
    margin-top: 15px;
  }
</style>
